package com.oj.core.entity;

import com.oj.api.core.domain.ProblemInfo;
import com.oj.api.core.domain.ProblemSetInfo;
import com.oj.common.data.BaseEntity;
import lombok.*;

import javax.persistence.*;
import java.util.*;

/**
 * 题目集实体
 * Created by Bing on 2018/5/4.
 */
@Entity(name = "CORE_PROBLEM_SET")
@Table(indexes = {
        @Index(name = "core_idx_code_problem_set", unique = true, columnList = "code"),
        @Index(name = "pms_idx_updatetime_problem_set", columnList = "UPDATE_TIME")
})
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ProblemSetEntity extends BaseEntity<ProblemSetInfo>{

    /**
     * 单位
     */
    private String company;
    /**
     * 类型
     */
    private Integer type;

    /**
     * 时间类型
     */
    private Integer timeType;
    /**
     * 开始时间
     */
    private Date startTime;
    /**
     * 结束时间
     */
    private Date endTime;
    /**
     * 公告
     */
    private String notice;
    /**
     * 编译器
     */
    private String originCode;
    /**
     * 是否公开
     */
    private int open;
    /**
     * 题库集大类
     */
    @ManyToOne(cascade = {CascadeType.REFRESH},fetch = FetchType.LAZY)
    @JoinColumn(name = "PSCE_ID")
    private ProblemSetCategoryEntity problemSetCategoryEntity;


    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinTable(name = "CORE_PSE_PE", joinColumns = @JoinColumn(name = "PSE_ID", referencedColumnName = "PK_ID"),
            inverseJoinColumns = @JoinColumn(name = "PE_ID", referencedColumnName = "PK_ID"))
    @OrderBy(value = "company")
    private Set<ProblemEntity> problems;


    @Override
    protected ProblemSetInfo createInfo() {
        return new ProblemSetInfo();
    }

    @Override
    public ProblemSetInfo buildInfo(ProblemSetInfo problemSetInfo) {
        super.buildInfo(problemSetInfo);
        List<ProblemInfo> lines = new ArrayList<>();
        if(this.getProblems()!=null){
            this.getProblems().forEach(a->{
                lines.add(a.buildInfo());
            });
        }
        problemSetInfo.setProblems(lines);
        return problemSetInfo;
    }

    public void addProblem(ProblemEntity problemEntity){
        if(problems==null){
            problems = new HashSet<>();
        }
        problems.add(problemEntity);
    }
}
